﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.shell.view {
    import com.jfdesgagne.jfdesgagne09.shell.ApplicationFacade;
	import com.jfdesgagne.jfdesgagne09.shell.model.LocaleProxy;
    import com.jfdesgagne.jfdesgagne09.shell.view.component.Loading;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Site;
	import com.jfdesgagne.jfdesgagne09.shell.view.SiteMediator;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Site;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import gs.TweenLite;
    
    import org.puremvc.as3.multicore.interfaces.*;
    import org.puremvc.as3.multicore.patterns.mediator.Mediator;
    
    public class LoadingMediator extends Mediator implements IMediator {
        // Cannonical name of the Mediator
        public static const NAME:String = "LoadingMediator";

        public function LoadingMediator($viewComponent:Object) {
			super(NAME, $viewComponent);
        }
		
		public static function getInstance():LoadingMediator {
			return ApplicationFacade.getInstance(ApplicationFacade.NAME).retrieveMediator(LoadingMediator.NAME) as LoadingMediator;
		}
		
		override public function listNotificationInterests():Array {
            return [ 
            		ApplicationFacade.INITIALIZE,
					ApplicationFacade.RESIZE
                   ];
        }

        override public function handleNotification($note:INotification):void {
            switch ($note.getName()) {
            	case ApplicationFacade.INITIALIZE:    	
					this._initializeLoading();
				case ApplicationFacade.RESIZE:
					this.loading.resize();
                break;
            }
        }
			
		private function _initializeLoading():void {
			this.loading.loadingText_mc.title_txt.text = LocaleProxy.getString("IDS_LOADING_TITLE");
			var $site:Site = SiteMediator.getInstance().site;
			var $site_bd:BitmapData = new BitmapData(this.loading.stage.stageWidth, this.loading.stage.stageHeight, false);
				$site_bd.draw($site);
			var $site_bmp:Bitmap = new Bitmap($site_bd);
			this.loading.background_mc.addChild($site_bmp);
		}
		
		public function set percent($percent:Number):void {
			trace("percent:" + $percent);
			this.loading.loadingText_mc.percent_txt.text = $percent;
		}
		
		public function set loadingText($text:String):void {
			this.loading.loadingText_mc.text_txt.text = $text;
		}
		
		public function show():void {
			TweenLite.to(this.loading, 1, {alpha:1});
		}
		
		public function hide():void {
			TweenLite.to(this.loading, 1, {alpha:0});
		}
        
		public function get loading():Loading {
            return this.viewComponent as Loading;
        }
    }
}